import Vue from 'vue'
import store from '../store'
import VueRouter from 'vue-router'
//懒加载方式导入组件
const commonLayout = () =>
    import ('../components/commonLayout.vue')
const Lists = () =>
    import ('../views/Lists/Lists.vue')
const Home = () =>
    import ('@/views/home/home.vue')
const Login = () =>
    import ('@/views/Login/login.vue')
const detail = () =>
    import ('@/views/home/detail.vue')
const detailLun = () =>
    import ('@/views/home/detailLun.vue')
const personal = () =>
    import ('@/views/home/personal.vue')
const silinsi = () =>
    import ('@/views/404.vue')
const timeLocus = () =>
    import ('@/views/timer/timeLocus.vue')
const recommend = () =>
    import ('@/views/recommend/recommend.vue')
const aboutMe = () =>
    import ('../views/me/aboutMe.vue')
const article = () =>
    import ('../views/Administration/Administration.vue')
const articleHome = () =>
    import ('@/components/Administration/Home/home.vue')
const jurisdiction = () =>
    import ('@/components/jurisdiction.vue')
const management = () =>
    import ('@/components/Administration/management/index.vue')
const Add = () =>
    import ('@/components/Administration/add/add.vue')
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
Vue.use(VueRouter)

const routes = [{
        path: '/',
        component: commonLayout,
        children: [{
            path: '',
            component: Home
        }, {
            path: '/Lists',
            name: 'Lists',
            component: Lists,
        }, {
            path: '/personal',
            name: 'personal',
            component: personal,
            meta: {
                // 需要登录权限
                requireAuth: true
            }
        }, {
            path: '/detail/:id',
            name: 'detail',
            component: detail
        }, {
            path: '/detailLun/:id',
            name: 'detail',
            component: detailLun
        }, {
            path: '/timeLocus',
            name: 'timeLocus',
            component: timeLocus
        }, {
            path: '/recommend',
            name: 'recommend',
            component: recommend
        }, {
            path: '/aboutMe',
            name: 'aboutMe',
            component: aboutMe
        }]
    }, {
        path: '/login',
        name: 'login',
        component: Login
    },
    {
        path: '/article',
        name: 'article',
        component: article,
        meta: {
            // 需要登录权限
            requireAuth: true
        },
        children: [{
            path: '/',
            name: 'articleHome',
            component: articleHome
        }, {
            path: 'management',
            name: 'management',
            component: management
        }, {
            path: 'add',
            name: 'add',
            component: Add
        }]
    }, {
        path: '/jurisdiction',
        name: 'jurisdiction',
        component: jurisdiction
    }, {
        path: '/404',
        name: '404',
        component: silinsi
    },
    // 错误地址重定向
    {
        path: "*",
        redirect: "/404"
    }

]

const router = new VueRouter({
    routes,

})



// 路由守卫 -> 进去路由前
router.beforeEach((to, from, next) => {
    // 让页面回到顶部
    window.pageYOffset = document.documentElement.scrollTop = 0;
    document.body.scrollTop = document.documentElement.scrollTop = 0;
    scrollTo(0, 0);
    document.documentElement.scrollTop = document.body.scrollTop = 0;
    NProgress.start()
        // 由于刷新会丢失登录状态，所以刷新后要从sessionStorage里获取token再次存放在vuex
    store.commit('setToken', sessionStorage.getItem('token') || "")
        //  判断有无token，有则设置当前状态为登录状态
    if (store.state.token) {
        store.commit('changIsSignIn', 1)
    }
    //  先判断去的页面是否需要登录权限
    if (to.meta.requireAuth) {
        // 判断有没有登录
        if (store.state.token) {
            // 有登录 ,则判断去的路由是否为 我的博客
            // 去我的博客之前拦截，判断当前用户是否为管理员，如果是管理员，则next()
            if (to.name === 'article') {
                let nickname = sessionStorage.getItem("nickname");

                if (nickname === 'MACH') {
                    next()
                } else {
                    // 否则直接返回到权限页面
                    next({ name: 'jurisdiction' })
                }
            } else {
                next()
            }
        } else {
            // 未登录则去登录
            if (to.name === 'login') {
                next()
            } else {
                next({ name: 'login' })
            }
        }
    } else {
        // 不需要登录权限的页面直接next
        next()
    }
})
router.afterEach(() => {
    NProgress.done()
})
export default router